﻿#region Using

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Collections.Generic;
using System.Web.UI;
using SubjectWorkShop.Core;


#endregion

public partial class _default : SubjectWorkShop.Core.Web.Controls.BlogBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsCallback)
            return;

        if (Request.RawUrl.ToLowerInvariant().Contains("/category/"))
        {
            DisplayCategories();
        }
        else if (Request.RawUrl.ToLowerInvariant().Contains("/author/"))
        {
            DisplayAuthors();
        }
        else if (Request.RawUrl.ToLowerInvariant().Contains("?tag="))
        {
            DisplayTags();
        }
        else if (Request.QueryString["year"] != null || Request.QueryString["date"] != null || Request.QueryString["calendar"] != null)
        {
            if (Request.RawUrl.Contains("year="))
                Redirect();
            else
                DisplayDateRange();
        }
        else if (Request.QueryString["apml"] != null)
        {
            DisplayApmlFiltering();
        }
        else if (Request.QueryString.Count == 0 || !string.IsNullOrEmpty(Request.QueryString["page"]) || !string.IsNullOrEmpty(Request.QueryString["theme"]) || !string.IsNullOrEmpty(Request.QueryString["blog"]))
        {

        }

        AddMetaKeywords();
        base.AddMetaTag("description", Server.HtmlEncode("BlogSettings.Instance.Description"));
        base.AddMetaTag("author", Server.HtmlEncode("BlogSettings.Instance.AuthorName"));
    }

    private void DisplayApmlFiltering()
    {
        Uri url = null;
        if (Uri.TryCreate(Request.QueryString["apml"], UriKind.Absolute, out url))
        {
            Page.Title = "APML filtered list";
    
        }

    }

    private void InvalidateCache()
    {
        HttpResponse.RemoveOutputCacheItem(Utils.RelativeWebRoot + "default.aspx");
    }

    /// <summary>
    /// Permanently redirects to the correct URL format if the page is requested with
    /// the old URL: /default.aspx?year=2007&month=12
    /// <remarks>
    /// The redirection is important so that we don't end up having 2 URLs 
    /// to the same resource. It's for SEO purposes.
    /// </remarks>
    /// </summary>
    private void Redirect()
    {
        string year = Request.QueryString["year"];
        string month = Request.QueryString["month"];
        string date = Request.QueryString["date"];
        string page = string.IsNullOrEmpty(Request.QueryString["page"]) ? string.Empty : "?page=" + Request.QueryString["page"];
        string rewrite = null;

        if (!string.IsNullOrEmpty(date))
        {
            DateTime dateParsed = DateTime.Parse(date);
            rewrite = Utils.RelativeWebRoot + dateParsed.Year + "/" + dateParsed.Month + "/" + dateParsed.Day + "/default.aspx";
        }
        else if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))
        {
            rewrite = Utils.RelativeWebRoot + year + "/" + month + "/default.aspx";
        }
        else if (!string.IsNullOrEmpty(year))
        {
            rewrite = Utils.RelativeWebRoot + year + "/default.aspx";
        }

        if (rewrite != null)
        {
            Response.Clear();
            Response.StatusCode = 301;
            Response.AppendHeader("location", rewrite + page);
            Response.End();
        }
    }

    private static readonly Regex YEAR_MONTH = new Regex("/([0-9][0-9][0-9][0-9])/([0-1][0-9])", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    private static readonly Regex YEAR_MONTH_DAY = new Regex("/([0-9][0-9][0-9][0-9])/([0-1][0-9])/([0-3][0-9])", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    /// <summary>
    /// Adds the post's tags as meta keywords.
    /// </summary>
    private void AddMetaKeywords()
    {
        
    }

    private void DisplayCategories()
    {
        
    }

    private void DisplayAuthors()
    {

    }

    private void DisplayTags()
    {

    }

    private void DisplayDateRange()
    {
        
    }

    //private void DisplayDateRange(string year, string month, string day)
    //{
    //  if (string.IsNullOrEmpty(day))
    //  {
    //    DateTime dateFrom = DateTime.Parse(year + "-" + month + "-01", CultureInfo.InvariantCulture);
    //    DateTime dateTo = dateFrom.AddMonths(1).AddMilliseconds(-1);
    //    PostList1.Posts = Post.GetPostsByDate(dateFrom, dateTo);
    //    Title = BlogSettings.Instance.Name + " - " + dateFrom.ToString("MMMM yyyy");
    //  }
    //  else
    //  {
    //    DateTime date = DateTime.Parse(year + "-" + month + "-" + day, CultureInfo.InvariantCulture);
    //    PostList1.Posts = Post.GetPostsByDate(date, date);
    //    Title = BlogSettings.Instance.Name + " - " + date.ToString("MMMM d. yyyy");
    //  }
    //}

}
